<template>
  <div class="mt20 df fdc h100">
    <div class="f1 df fdc">
      <el-form>
        <el-form-item>
          <div class="df aic w100">
            <el-input
              style="width: 30%"
              class="mr20"
              placeholder="请输入用户昵称，手机号进行搜索"
              v-model="search_content"
            ></el-input>
            <el-button @click="handleSearch" type="primary">搜索</el-button>
            <el-button @click="reset">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
      <el-table class="f1" :data="tableData">
        <el-table-column prop="id" label="用户ID"></el-table-column>
        <el-table-column prop="nickname" label="用户昵称"></el-table-column>
        <el-table-column
          prop="create_time_text"
          label="创建时间"
        ></el-table-column>
        <el-table-column
          prop="update_time_text"
          label="更新时间"
        ></el-table-column>
        <el-table-column prop="phone" label="手机号"></el-table-column>
        <el-table-column
          prop="upload_count"
          label="上传内容数量"
        ></el-table-column>
        <el-table-column prop="user_profile" label="用户画像"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button @click="deleteUser(scope.row)" link>注销</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 40]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="margin-top: 20px"
    />
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { userList, userDel } from "@/apis/user";
import { transDate } from "@/utils/transforDate";
import { ElMessage, ElMessageBox } from "element-plus";

const tableData = ref([1]);
const pageSize = ref(10);
const currentPage = ref(1);
const total = ref(0);
const search_content = ref("");

const handleSizeChange = (e) => {
  getData(currentPage.value, search_content.value);
};

const handleCurrentChange = (e) => {
  getData(currentPage.value, search_content.value);
};

const getData = async (page, val) => {
  try {
    let data = {};
    if (page) {
      data.page = page;
    }
    data.page_size = pageSize.value;
    if (val) {
      data.q = val;
    }
    let res = await userList(data);
    if (res.data.code == 1002) {
      tableData.value = res.data.data.items;
      tableData.value = tableData.value.map((item) => {
        item.create_time_text = transDate(item.created_at);
        item.update_time_text = transDate(item.updated_at);
        return item;
      });
    }
  } catch (e) {
    console.log(e);
  }
};

const handleSearch = () => {
  getData(currentPage.value, search_content.value);
};

const deleteUser = (e) => {
  ElMessageBox.confirm("确定注销用户？", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  })
    .then(async () => {
      try {
        let data = {};
        data.id = e.id;
        data.is_del = 0;
        let res = await userDel(data);
        if (res.data.code == 200) {
          ElMessage.success("注销成功");
          getData(currentPage.value, search_content.value);
        } else {
          ElMessage.info(res.data.message);
        }
      } catch (e) {}
    })
    .catch(() => {});
};

const reset = () => {
  search_content.value = "";
  getData(currentPage.value, search_content.value);
};
onMounted(() => {
  getData(currentPage.value);
});
</script>

<style lang="scss" scoped></style>
